#include <STC89C5xRC.H>

sbit I2C_SCL=P2^1;
sbit I2C_SDA=P2^0;

//i2c开始
void i2c_start()
{
	I2C_SDA=1;
	I2C_SCL=1;
	I2C_SDA=0;
	I2C_SCL=0;
}

//i2c停止
void i2c_stop()
{
	I2C_SDA=0;
	I2C_SCL=1;
	I2C_SDA=1;
}

//i2c发送一个字节
void i2c_sendbyte(unsigned char byte)
{
	unsigned char i;
	for(i = 0;i<8;i++)
	{
		I2C_SDA=byte&(0x80>>i);
		I2C_SCL=1;
		I2C_SCL=0;
	}
	
}

//i2c接受一个字节
unsigned char i2c_receivebyte()
{
	unsigned char byte=0x00;
	unsigned char i;
	I2C_SDA=1;
	for(i=0;i<8;i++)
	{
		I2C_SCL=1;
		if(I2C_SDA){byte|=(0x80>>i);}
		I2C_SCL=0;
	}
	return byte;
}

//i2c发送应答位  ack-应答
//0为应答，1为非应答
void i2c_sendack(unsigned char ackbit)
{
	I2C_SDA = ackbit;
	I2C_SCL=1;
	I2C_SCL=0;
}

//i2c接收应答位  ack-应答
//0为应答，1为非应答
unsigned char i2c_receiveack()
{
	unsigned char ackbit;
	I2C_SDA=1;
	I2C_SCL=1;
	ackbit=I2C_SDA;
	I2C_SCL=0;
	return ackbit;
}
